package com.xii.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.xii.mapper.MemberMapper;
import com.xii.mapper.OrderMapper;
import com.xii.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Map<String, Object> getMemberReport() {

        //返回集合
        HashMap<String, Object> report = new HashMap<>();
        //存储月份
        ArrayList<String> xReport = new ArrayList<>();
        //存储会员数量
        ArrayList<Integer> yReport = new ArrayList<>();

        //填充x轴   穿越
        Calendar date = Calendar.getInstance();
        //提前12个月
        date.add(Calendar.MONTH,-12);
        for (int i = 0; i < 12; i++) {
            date.add(Calendar.MONTH,1);
            Date time = date.getTime();
            xReport.add(new SimpleDateFormat("yy-MM").format(time));
        }

        for (String months : xReport) {
            months = months + "-31";
            Integer count = memberMapper.getMemberCountByMonth(months);
            yReport.add(count);
        }
        report.put("months",xReport);
        report.put("memberCount",yReport);
        return report;
    }

    /**
     * 运营数据统计
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        //获取当前日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一的日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获取本月的第一天
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //今日新增会员数
        Integer todayNewMember = memberMapper.findMemberCountByDate(today);
        //总会员数         
        Integer totalMember = memberMapper.findMemberTotalCount();
        //本周新增会员数         
        Integer thisWeekNewMember = memberMapper.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数         
        Integer thisMonthNewMember = memberMapper.findMemberCountAfterDate(firstDay4ThisMonth);
        //今日预约数
        Integer todayOrderNumber = orderMapper.findOrderCountByDate(today);
        //本周预约数         
        Integer thisWeekOrderNumber = orderMapper.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数         
        Integer thisMonthOrderNumber = orderMapper.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = orderMapper.findVisitsCountByDate(today);
        //本周到诊数         
        Integer thisWeekVisitsNumber = orderMapper.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderMapper.findVisitsCountAfterDate(firstDay4ThisMonth);
        //热门套餐（取前4）         
        List<Map> hotSetmeal = orderMapper.findHotSetmeal();
        Map<String,Object> map = new HashMap<>();
        map.put("reportDate",today);
        map.put("todayNewMember",todayNewMember);
        map.put("totalMember",totalMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        map.put("todayOrderNumber",todayOrderNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        map.put("hotSetmeal",hotSetmeal);
        return map;
    }
}
